home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Back to Campus
/
Back to Campus - Computer Life (Ziff-Davis)(1995).ISO
/
shgmdemo
/
msinst
/
wing
/
dumb3d.hp_
/
dumb3d.hp
Wrap
Text File
|
1994-08-14
|
10KB
|
433 lines
/**************************************************************************
dumb3d.hpp - A simple linear algebra library for 3D.
**************************************************************************/
/**************************************************************************
(C) Copyright 1994 Microsoft Corp. All rights reserved.
You have a royalty-free right to use, modify, reproduce and
distribute the Sample Files (and/or any modified version) in
any way you find useful, provided that you agree that
Microsoft has no warranty obligations or liability for any
Sample Application Files which are modified.
**************************************************************************/
#if !defined(DUMB3D_HPP)
#define DUMB3D_HPP
/*----------------------------------------------------------------------------
This header contains the declarations for the dumb3d functions.
*/
// real type
typedef float real;
#define M_PI 3.14159265358979323846
// forward declarations
class point_4;
class vector_4;
class matrix_4x4;
/*----------------------------------------------------------------------------
globally useful functions.
*/
inline vector_4 operator-( point_4 const &Operand1, point_4 const &Operand2 );
inline vector_4 operator+( vector_4 const &Operand1,
vector_4 const &Operand2 );
inline vector_4 operator-( vector_4 const &Operand1,
vector_4 const &Operand2 );
inline vector_4 operator*( vector_4 const &Multiplicand,
real const &Multiplier );
inline vector_4 operator*( real const &Multiplier,
vector_4 const &Multiplicand );
inline point_4 operator+( point_4 const &Operand1, vector_4 const &Operand2 );
inline point_4 operator-( point_4 const &Operand1, vector_4 const &Operand2 );
inline point_4 operator+( vector_4 const &Operand2, point_4 const &Operand1 );
inline vector_4 operator-( vector_4 const &Operand1 );
inline vector_4 CrossProduct( vector_4 const &Operand1,
vector_4 const &Operand2 );
inline real DotProduct( vector_4 const &Operand1, vector_4 const &Operand2 );
matrix_4x4 operator*( matrix_4x4 const &Multiplicand,
matrix_4x4 const &Multiplier );
vector_4 operator*( matrix_4x4 const &Multiplicand,
vector_4 const &Multiplier );
point_4 operator*( matrix_4x4 const &Multiplicand,
point_4 const &Multiplier );
/*----------------------------------------------------------------------------
quadruple. Base class for homogeneous vectors and points.
*/
class quadruple
{
public:
inline real GetElement( int Row ) const;
inline real GetX( void ) const;
inline real GetY( void ) const;
inline real GetZ( void ) const;
inline real GetW( void ) const;
inline void SetElement( int Row, real Value );
inline void SetX( real Value );
inline void SetY( real Value );
inline void SetZ( real Value );
inline void SetW( real Value );
protected:
inline quadruple( void );
inline quadruple( real X, real Y, real Z, real W );
inline quadruple( quadruple const & );
inline quadruple &operator=( quadruple const & );
real aElements[4];
};
/*----------------------------------------------------------------------------
point_4. This class represents a homogeneous 3D point.
*/
class point_4 :
public quadruple
{
public:
inline point_4( void );
inline point_4( real X, real Y, real Z );
inline void Homogenize( void );
};
/*----------------------------------------------------------------------------
vector_4. This class represents a homogeneous 3D vector.
*/
class vector_4 :
public quadruple
{
public:
inline vector_4( void );
inline vector_4( real X, real Y, real Z );
vector_4 &Normalize( void );
};
/*----------------------------------------------------------------------------
matrix_4x4. This class represents row major 4x4 homogeneous matrices.
*/
class matrix_4x4
{
public:
matrix_4x4( void );
matrix_4x4 &ConcatenateXRotation( real Degrees );
matrix_4x4 &ConcatenateYRotation( real Degrees );
matrix_4x4 &ConcatenateZRotation( real Degrees );
matrix_4x4 &ConcatenateXTranslation( real Distance );
matrix_4x4 &ConcatenateYTranslation( real Distance );
matrix_4x4 &ConcatenateZTranslation( real Distance );
inline real GetElement( int Row, int Column ) const;
inline matrix_4x4 &SetElement( int Row, int Column, real Value );
protected:
enum do_not_initialize { DoNotInitialize };
inline matrix_4x4( do_not_initialize );
real aElements[4][4];
};
/*----------------------------------------------------------------------------
view transform.
*/
class view_transform :
public matrix_4x4
{
public:
view_transform( point_4 const &Viewpoint, vector_4 const &ViewDirection,
vector_4 const &Up );
};
/*----------------------------------------------------------------------------
inline function definitions.
*/
inline vector_4 operator-( point_4 const &Operand1, point_4 const &Operand2 )
{
return vector_4(Operand1.GetX() - Operand2.GetX(),
Operand1.GetY() - Operand2.GetY(),
Operand1.GetZ() - Operand2.GetZ());
}
inline vector_4 operator+( vector_4 const &Operand1,
vector_4 const &Operand2 )
{
return vector_4(Operand1.GetX() + Operand2.GetX(),
Operand1.GetY() + Operand2.GetY(),
Operand1.GetZ() + Operand2.GetZ());
}
inline vector_4 operator-( vector_4 const &Operand1,
vector_4 const &Operand2 )
{
return vector_4(Operand1.GetX() - Operand2.GetX(),
Operand1.GetY() - Operand2.GetY(),
Operand1.GetZ() - Operand2.GetZ());
}
inline vector_4 operator-( vector_4 const &Operand1 )
{
return vector_4(-Operand1.GetX(),-Operand1.GetY(),-Operand1.GetZ());
}
inline vector_4 operator*( vector_4 const &Multiplicand,
real const &Multiplier )
{
return vector_4(Multiplicand.GetX() * Multiplier,
Multiplicand.GetY() * Multiplier,
Multiplicand.GetZ() * Multiplier);
}
inline vector_4 operator*( real const &Multiplier,
vector_4 const &Multiplicand )
{
return vector_4(Multiplicand.GetX() * Multiplier,
Multiplicand.GetY() * Multiplier,
Multiplicand.GetZ() * Multiplier);
}
inline point_4 operator+( point_4 const &Operand1, vector_4 const &Operand2 )
{
return point_4(Operand1.GetX() + Operand2.GetX(),
Operand1.GetY() + Operand2.GetY(),
Operand1.GetZ() + Operand2.GetZ());
}
inline point_4 operator-( point_4 const &Operand1, vector_4 const &Operand2 )
{
return point_4(Operand1.GetX() - Operand2.GetX(),
Operand1.GetY() - Operand2.GetY(),
Operand1.GetZ() - Operand2.GetZ());
}
inline point_4 operator+( vector_4 const &Operand1, point_4 const &Operand2 )
{
return Operand2 + Operand1;
}
inline vector_4 CrossProduct( vector_4 const &Operand1,
vector_4 const &Operand2 )
{
real X = Operand1.GetY() * Operand2.GetZ() -
Operand1.GetZ() * Operand2.GetY();
real Y = Operand1.GetZ() * Operand2.GetX() -
Operand1.GetX() * Operand2.GetZ();
real Z = Operand1.GetX() * Operand2.GetY() -
Operand1.GetY() * Operand2.GetX();
return vector_4(X,Y,Z);
}
inline real DotProduct( vector_4 const &Operand1, vector_4 const &Operand2 )
{
return Operand1.GetX() * Operand2.GetX() +
Operand1.GetY() * Operand2.GetY() +
Operand1.GetZ() * Operand2.GetZ();
}
inline real quadruple::GetElement( int Row ) const
{
return aElements[Row];
}
inline real quadruple::GetX( void ) const
{
return aElements[0];
}
inline real quadruple::GetY( void ) const
{
return aElements[1];
}
inline real quadruple::GetZ( void ) const
{
return aElements[2];
}
inline real quadruple::GetW( void ) const
{
return aElements[3];
}
inline void quadruple::SetElement( int Row, real Value )
{
aElements[Row] = Value;
}
inline void quadruple::SetX( real Value )
{
aElements[0] = Value;
}
inline vo